package com.eedi.framework.oauth2.dal.mapper;

import com.eedi.framework.common.enums.UserTypeEnum;
import com.eedi.framework.common.pojo.PageResult;
import com.eedi.framework.mybatis.core.mapper.BaseMapperX;
import com.eedi.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.eedi.framework.oauth2.controller.org.vo.token.OrgOAuth2AccessTokenPageReqVO;
import com.eedi.framework.oauth2.dal.dataobject.OrgOAuth2AccessTokenDO;
import org.apache.ibatis.annotations.Mapper;

import java.time.LocalDateTime;
import java.util.List;

@Mapper
public interface OrgOAuth2AccessTokenMapper extends BaseMapperX<OrgOAuth2AccessTokenDO> {

    default OrgOAuth2AccessTokenDO selectByAccessToken(String accessToken) {
        return selectOne(OrgOAuth2AccessTokenDO::getAccessToken, accessToken);
    }

    default List<OrgOAuth2AccessTokenDO> selectListByRefreshToken(String refreshToken) {
        return selectList(OrgOAuth2AccessTokenDO::getRefreshToken, refreshToken);
    }

    default PageResult<OrgOAuth2AccessTokenDO> selectPage(OrgOAuth2AccessTokenPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<OrgOAuth2AccessTokenDO>()
                .eqIfPresent(OrgOAuth2AccessTokenDO::getOrgUserType, reqVO.getUserId())
                .eqIfPresent(OrgOAuth2AccessTokenDO::getOrgUserType, reqVO.getUserType())
                .likeIfPresent(OrgOAuth2AccessTokenDO::getOrgOauth2ClientId, reqVO.getOrgOauth2ClientId())
                .gt(OrgOAuth2AccessTokenDO::getExpiresTime, LocalDateTime.now())
                .orderByDesc(OrgOAuth2AccessTokenDO::getOrgOauth2AccessTokenId));
    }

    default List<OrgOAuth2AccessTokenDO> selectByUserId(String userId, UserTypeEnum userType) {
        return selectList(new LambdaQueryWrapperX<OrgOAuth2AccessTokenDO>()
                .eqIfPresent(OrgOAuth2AccessTokenDO::getOrgUserId, userId)
                .eqIfPresent(OrgOAuth2AccessTokenDO::getOrgUserType, userType));
    }

}
